package com.bs.elasticsearch.controller;

import com.bs.common.utils.PageUtil;
import com.bs.common.utils.Result;
import com.bs.elasticsearch.service.ReadCsvToElasticService;
import com.bs.elasticsearch.vo.*;
import com.bs.elasticsearch.vo.param.NearByPoiParam;
import com.bs.elasticsearch.vo.param.SearchNamePoiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author bingshao
 * @date 2023/4/7
 **/
@RestController
@RequestMapping("/csv")
public class ReadCsvToElasticController {

    @Resource
    private ReadCsvToElasticService readCsvToElasticService;

    @PostMapping("/createIndex")
    public void createIndex() {
        readCsvToElasticService.createIndex();
    }

    @PostMapping("/readCsv")
    public void readCsv(@RequestPart("file") MultipartFile file, @ModelAttribute AddressIdVo addressIdVo) throws IOException {
        readCsvToElasticService.readCsv(file, addressIdVo);
    }

    @GetMapping("/geoSearch/{id}")
    public Result<PageUtil<List<LocationVo>>> geoSearch(@PathVariable("id") Long id) {
        PageUtil<List<LocationVo>> listPageUtil = readCsvToElasticService.geoSearch(id);
        return Result.success(listPageUtil);
    }

    @GetMapping("/geoByLatAndLon/{lon}/{lat}")
    public Result<GeoLocationAreaVo> geoByLatAndLon(@PathVariable("lon") Double lon, @PathVariable("lat") Double lat) throws IOException {
        GeoLocationAreaVo geoLocationAreaVo = readCsvToElasticService.geoByLonAndLat(lon, lat);
        return Result.success(geoLocationAreaVo);
    }

    @PostMapping("/readPoiCsv")
    public void readPoiCsv(@RequestPart("file") MultipartFile file) throws Exception {
        readCsvToElasticService.readPoiCsv(file);
    }

    @PostMapping("/getNearByPoi")
    public Result<List<GeoLocationPoiVo>> getNearByPoi(@RequestBody NearByPoiParam nearByPoiParam) {
        List<GeoLocationPoiVo> geoLocationPoiVoList = readCsvToElasticService.getNearByPoi(nearByPoiParam);
        return Result.success(geoLocationPoiVoList);
    }

    @PostMapping("/searchNamePoi")
    public Result<List<GeoLocationPoiVo>> searchNamePoi(@RequestBody SearchNamePoiParam searchNamePoiParam) {
        List<GeoLocationPoiVo> geoLocationPoiVoList = readCsvToElasticService.searchNamePoi(searchNamePoiParam);
        return Result.success(geoLocationPoiVoList);
    }

    @PostMapping("/getByAreaCode")
    public Result<List<GeoLocationVo>> getByAreaCode(@RequestBody List<String> areaCodeList) {
        List<GeoLocationVo> geoLocationVoList = readCsvToElasticService.getByAreaCode(areaCodeList);
        return Result.success(geoLocationVoList);
    }

}